package org.jeecgframework.web.material.service.impl;
import java.util.Date;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.web.material.entity.ProductInputEntity;
import org.jeecgframework.web.material.entity.ProductStoreEntity;
import org.jeecgframework.web.material.service.ProductInputServiceI;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("productInputService")
@Transactional
public class ProductInputServiceImpl extends CommonServiceImpl implements ProductInputServiceI {

	/**
	 * 入库
	 */
	public void saveProduct(ProductInputEntity pi) {
		TSUser user = ResourceUtil.getSessionUserName();
		pi.setCompanyId(user.getCompany());
		pi.setInUser(user);
		pi.setInDate(new Date());
		this.save(pi);
		
		//入库类型 1-正式入库  2-预入库
		String inputType = pi.getBeforeInput();
		Integer inputAmount = pi.getInputAmount();
		String pid = pi.getProductId().getId();
		if("1".equals(inputType)){//正式入库
			ProductStoreEntity store = super.findUniqueByProperty(ProductStoreEntity.class, "productId.id", pid);
			if(store!=null){
				Integer amount = store.getAmount();
				int newAmount = amount.intValue()+inputAmount.intValue();
				store.setAmount(newAmount);
				super.saveOrUpdate(store);
			}
		}
	}
	
	/**
	 * 预入库到正式入库
	 * @param pi
	 */
	public void beforeToStore(ProductInputEntity pi) {
		TSUser user = ResourceUtil.getSessionUserName();
		pi.setCompanyId(user.getCompany());
		pi.setInUser(user);
		pi.setInDate(new Date());
		pi.setBeforeInput("1");//入库类型 1-正式入库  2-预入库
		this.save(pi);
		
		Integer inputAmount = pi.getInputAmount();
		String pid = pi.getProductId().getId();
		ProductStoreEntity store = super.findUniqueByProperty(ProductStoreEntity.class, "productId.id", pid);
		if(store!=null){
			Integer amount = store.getAmount();
			int newAmount = amount.intValue()+inputAmount.intValue();
			store.setAmount(newAmount);
			super.saveOrUpdate(store);
		}
	}

}